package com.open.server.api.tool;

import com.open.common.utils.ExcelUtils;
import com.open.tool.dispatch.IGenerateDispatch;
import com.open.tool.dispatch.IServerLogDispatch;
import com.open.common.model.Result;
import com.open.tool.model.ServerLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 *
 * 服务器日志
 *
 * @author dashu03
 * @date 2025年06月07日 17:04
 * @description
 */
@RestController
@RequestMapping("/tool/serverLog")
public class ServerLogApi {

    @Autowired
    private IServerLogDispatch serverLogDispatch;

    @Autowired
    private IGenerateDispatch generateDispatch;

    /**
     * 下载日志
     * @param response
     */
    @PostMapping("downloadLog")
    public void downloadLog(HttpServletResponse response) {
        byte[] bytes = serverLogDispatch.downloadLog();
        //saveAsFile(bytes, "open-sys.zip");
        ExcelUtils.genCode(response, bytes, "open-sys.zip");
    }

    /**
     * 清空日志
     * @return
     */
    @GetMapping("deleteLog")
    public Result<Boolean> clearLog() {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(serverLogDispatch.clearLog());
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 预览日志数据
     * @return
     */
    @GetMapping("getServerLog")
    public Result<ServerLog> getServerLog() {
        long now = System.currentTimeMillis();
        Result<ServerLog> result = new Result<>();
        result.setData(serverLogDispatch.getServerLog());
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }
}
